package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.awt.geom.RectangularShape;
import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;
    //套餐列表
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.套餐条件分页查询
        // 1.1先构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        // 1.2模糊查询并判断是否有这个菜品
        qw.like(StrUtil.isNotBlank(name),Setmeal::getName,name);
        // 1.3开启分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        // 1.4分页查询(调用mapper查询)
        page = setmealMapper.selectPage(page,qw);

        // 1.5遍历套餐列表
        for (Setmeal setmeal : page.getRecords()) {
            // 2.根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            // 3.根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> sqw = new LambdaQueryWrapper<>();
            sqw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(sqw);
            setmeal.setSetmealDishes(setmealDishes);
        }

        return page;
    }

    //新增套餐
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void save(Setmeal setmeal) {
        //1.新增套餐
      setmealMapper.insert(setmeal);
      log.info("套餐主键返回:"+setmeal.getImage());
      // 2.遍历前端提交的套餐菜品列表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存中间表
            setmealDishMapper.insert(setmealDish);
        }
    }

    // 回显套餐
    @Override
    public Setmeal findById(Long id) {
        // 1.先查套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        // 2.根据套餐id查询菜品
        // 2.1构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        // 2.2 调用mapper
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        // 3.将菜品设置到套餐
        setmeal.setSetmealDishes(setmealDishList);
        // 返回套餐
        return setmeal;
    }

    //修改套餐
    @Override
    public void updateById(Setmeal setmeal) {
        // 1.修改套餐基本信息'
        setmealMapper.updateById(setmeal);
        // 2.根据套餐id删除原来的菜品
        // 2.1 构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(qw);
        // 3.遍历新增菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //菜品新增
            setmealDishMapper.insert(setmealDish);
        }
    }

    //删除套餐
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void deleteBatch(List<Long> ids) {
        // 1.先查询套餐状态，是否有启售 select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids); // id in(1,3,5)
        qw.eq(Setmeal::getStatus,1);// status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("套餐中有起售状态,请先停售再删除~~");
        }
        // 2.删套餐
        setmealMapper.deleteBatchIds(ids);
        // 3.删除套餐内菜品中间表
        LambdaQueryWrapper<SetmealDish> sqw = new LambdaQueryWrapper<>();
        sqw.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(sqw);
    }

    //停售套餐
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateStatus(Integer status, List<Long> ids) {
        //条件对象
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        //修改实体
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //调用mapper
        setmealMapper.update(setmeal,qw);
    }

    //套餐显示
    @Cacheable(value = "setmeal",key = "#categoryId")
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        log.info("支持查询MySQL");
        // 1/构建查询条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        //执行查询
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        return setmealList;//return setmealMapper.selectList(qw);链式编程
    }

    //套餐显示图片信息
    @Override
    public List<Dish> show(Long id) {
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);

        //创建一个集合,接收数据
        ArrayList<Dish> dishList = new ArrayList<>();

        //遍历集合得到菜品表
        for (SetmealDish setmealDish : setmealDishList) {

            //根据菜品表查看详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());
            dishList.add(dish);
        }
        return dishList;


    }


}
